package com.gxa.modules.service.accessservice.impl;

import com.gxa.common.R;
import com.gxa.common.utils.MD5Utils;
import com.gxa.modules.mapper.accessmapper.UserMapper;
import com.gxa.modules.pojo.Role;
import com.gxa.modules.pojo.User;
import com.gxa.modules.service.accessservice.UserService;
//import com.gxa.modules.shiro.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.ArrayList;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public R selectUser(String userName,String page,String size) {
        R<Object> r = new R<>();
        int statrIndex=(Integer.parseInt(page)-1)*Integer.parseInt(size);
        int endIndex=Integer.parseInt(page)*Integer.parseInt(size);
        r.setCode(200);
        r.setMsg("查询成功！");
        r.setData(userMapper.selectUser(userName,statrIndex,endIndex));
        return r;
    }

    @Override
    public R insertUser(User user) {
        R<Object> r = new R<>();

        r.setCode(500);
        r.setMsg("新增失败！");
//        user.setPassword(ShiroUtil.encrypt(user.getPassword(), "gxa"));
        user.setPassword(MD5Utils.encrypt(user.getPassword(),MD5Utils.MD5_KEY));

        if (userMapper.insertUser(user)>0){

            r.setCode(200);
            r.setMsg("新增成功！");
        }
        return r;
    }

    @Override
    public R deleteUser(String userId) {
        R<Object> r = new R<>();

        if ( userId ==null || "".equals(userId) ){
            r.setCode(500);
            r.setMsg("请选择要删除的信息！");
            return r;
        }

        r.setCode(500);
        r.setMsg("删除失败！");

        String[] strings = userId.split(",");
        for (int i=0;i<strings.length;i++) {
            userId=strings[i];
            if(userMapper.deleteUser(userId)>0){
                r.setCode(200);
                r.setMsg("删除成功！");
            }
        }


        return r;
    }

    @Override
    public R updateUser(User user) {
        R<Object> r = new R<>();

        r.setCode(500);
        r.setMsg("修改失败！");
        user.setPassword(MD5Utils.encrypt(user.getPassword(),MD5Utils.MD5_KEY));
        if (userMapper.updateUser(user)>0){
            r.setCode(200);
            r.setMsg("修改成功！");
        }
        return r;
    }
}
